package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.bean.ListBean;
import com.shd.edanpei.bean.NeedBean;
import com.shd.edanpei.presenter.contract.AddCarProductContract;
import com.shd.edanpei.presenter.AddCarProductPresenter;
import com.shd.edanpei.util.GsonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 新增产品
 */
public class AddProductActivity extends BaseActivity<AddCarProductPresenter>
        implements AddCarProductContract.AddCarProductIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.iv_type)
    ImageView ivType;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.et_auditTime)
    EditText etAuditTime;
    @BindView(R.id.tv_maxAmount)
    TextView tvMaxAmount;
    @BindView(R.id.tv_rate)
    TextView tvRate;
    @BindView(R.id.tv_age)
    TextView tvAge;
    @BindView(R.id.tv_carDistance)
    TextView tvCarDistance;
    @BindView(R.id.tv_carAge)
    TextView tvCarAge;
    @BindView(R.id.tv_houseAge)
    TextView tvHouseAge;
    @BindView(R.id.et_houseSize)
    EditText etHouseSize;
    //    @BindView(R.id.et_repayWay)
//    EditText etRepayWay;
    @BindView(R.id.tv_repayWay)
    TextView tvRepayWay;
    @BindView(R.id.tv_repayDate)
    TextView tvRepayDate;
    @BindView(R.id.tv_needData)
    TextView tvNeedData;
    @BindView(R.id.ll_car)
    View car;
    @BindView(R.id.ll_house)
    View house;

    private List<NeedBean> repayWayList = new ArrayList<>();
    private List<NeedBean> needBeanList = new ArrayList<>();
    private int mType;

    @Override
    protected int setLayout() {
        return R.layout.activity_product;
    }

    @Override
    protected AddCarProductPresenter bindPresenter() {
        return new AddCarProductPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);
        headline.setText("新增产品");

        //初始化布局数据
        Bundle extras = getIntent().getExtras();
        mType = extras.getInt(Constant.TYPE);
        car.setVisibility(mType == Constant.CAR ? View.VISIBLE : View.GONE);
        house.setVisibility(mType == Constant.HOUSE ? View.VISIBLE : View.GONE);
        ivType.setImageDrawable(getResources().getDrawable(mType == Constant.CAR ? R.drawable.ic_label_car : R.drawable.ic_label_house));
        tvType.setText(mType == Constant.CAR ? "车辆产品" : "住房产品");
    }

    @Override
    protected void initListener() {
        etAuditTime.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mPresenter.setAuditTime(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etHouseSize.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mPresenter.setHouseSize(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @BindView(R.id.tv_area)
    TextView tvArea;

    @OnClick(R.id.ll_area)
    void onArea() {
        mPresenter.setAreas(tvArea);
    }


    @OnClick(R.id.ll_maxAmount)
    void onMaxAmount() {
        mPresenter.setMaxAmount(tvMaxAmount);
    }


    @OnClick(R.id.ll_rate)
    void onRate() {
        mPresenter.setRate(tvRate);
    }


    @OnClick(R.id.ll_age)
    void onAge() {
        mPresenter.setAge(tvAge);
    }


    @OnClick(R.id.ll_carDistance)
    void onCarDisance() {
        mPresenter.setCarDistance(tvCarDistance);
    }

    @OnClick(R.id.ll_carAge)
    void onCarAge() {
        mPresenter.setCarAge(tvCarAge);
    }

    @OnClick(R.id.ll_houseAge)
    void onHouseAge() {
        mPresenter.setHouseAge(tvHouseAge);
    }


    @OnClick(R.id.ll_repayDate)
    void onRepayDate() {
        mPresenter.setRepayDate(tvRepayDate);
    }

    @OnClick(R.id.rl_needData)
    void onNeedData() {
        Bundle bundle = new Bundle();
        bundle.putString(Constant.TITLE, "所需材料");
        bundle.putInt(Constant.TAG, Constant.NEED_DATA);
        bundle.putString(Constant.DATA, GsonUtil.toJson(needBeanList));
        startActivity(SelectActivity.class, bundle);
    }

    /**
     * 归还方式
     */
    @OnClick(R.id.ll_repayWay)
    void onRepayWay() {
        Bundle bundle = new Bundle();
        bundle.putString(Constant.TITLE, "归还方式");
        bundle.putInt(Constant.TAG, Constant.REPAY_WAY);
        bundle.putString(Constant.DATA, GsonUtil.toJson(repayWayList));
        startActivity(SelectActivity.class, bundle);
    }

    @OnClick(R.id.tv_submit)
    void onSubmit() {
        if (mType == Constant.CAR) {
            mPresenter.submitCar();
        } else if (mType == Constant.HOUSE) {
            mPresenter.submitHouse();
        }
    }

    @Subscribe(code = SelectActivity.DONE_SELECT)
    public void doneSelect(ListBean listBean) {
        String data = "";
        for (NeedBean needBean : listBean.getNeedBeanList()) {
            if (needBean.isCheck()) {
                if (data.isEmpty()) {
                    data = needBean.getName();
                } else {
                    data = data + ";" + needBean.getName();
                }
            }
        }
        switch (listBean.getType()) {
            case Constant.NEED_DATA:
                needBeanList = listBean.getNeedBeanList();
                setText(tvNeedData, data);
                mPresenter.setNeedData(data);
                break;
            case Constant.REPAY_WAY:
                repayWayList = listBean.getNeedBeanList();
                setText(tvRepayWay, data);
                mPresenter.setRepayWay(data);
                break;
        }


    }


    @OnClick(R.id.iv_headline_back)
    void close() {
        finish();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.get().unRegister(this);
    }

    @Override
    public void setText(TextView text, String str) {
        text.setText(str);
        text.setTextColor(ContextCompat.getColor(this, R.color.black));
    }
}
